package adcp.network.http;

import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import adcp.network.beans.AdcpUpLoad;
import com.xkh.xkh.MyApplication;
import managers.VolleySingleton;

/**
 * 上传adcp数据
 * Created by nimi on 2015/10/28.
 */
public class SendAdcp {
    Delegate delegate;
    MsgBack back;

    public void sendAdcp() {

    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void send(final AdcpUpLoad adcp) {//新的请求执行上传adcp
        String ip = MyApplication.share().getString(R.string.IP);
//        String url ="http://192.168.23.1:8080/imServer/guanqu/RecordADCPValue";
        String url = "http://" + ip + "/guanqu/RecordADCPValue";
        RequestQueue requestQueue = VolleySingleton.
                getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                        if (delegate != null)
                            delegate.onResponse(response, SendAdcp.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.print(error);
                if (delegate != null)
                    delegate.onError(error, SendAdcp.this);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {//数据填充
                Map map = new HashMap();
                map.put("adcp.id", adcp.getId());
                map.put("adcp.name", adcp.getName());
                map.put("adcp.rate", adcp.getRate() + "");
                map.put("adcp.Water_level", adcp.getWater_level() + "");
                map.put("adcp.time", adcp.getTime());
                map.put("adcp.method", adcp.getMethod() + "");
                System.out.print(adcp.getMethod());
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        requestQueue.add(stringRequest);//加入请求队列
    }

    private void afterGet(String str) {
        back = JSON.parseObject(str, MsgBack.class);
        if (back.getStatus().equals(MsgBack.SUCCESS + "")) {
            Toast.makeText(MyApplication.share(), "上传成功", Toast.LENGTH_SHORT).show();
        } else if (back.getStatus().equals(MsgBack.FAILED + "")) {
            Toast.makeText(MyApplication.share(), "上传失败", Toast.LENGTH_SHORT).show();
        }
    }

    public MsgBack getBack() {
        return back;
    }

    public static interface Delegate {
        public void onResponse(String s, SendAdcp sendAdcp);

        public void onError(VolleyError volleyError, SendAdcp sendAdcp);
    }

    public static class MsgBack {
        private static final int SUCCESS = 1;
        private static final int FAILED = 0;
        public String status = "";

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }
}
